"""
日志相关的数据库操作函数
"""

from .db import get_db_connection, close_connection


async def add_log(user_id, content):
    """
    添加日志记录
    
    Args:
        user_id: 用户ID
        content: 日志内容
    
    Returns:
        bool: 是否添加成功
    """
    connection = None
    try:
        connection = await get_db_connection()
        async with connection.cursor() as cursor:
            sql = """
                INSERT INTO logs (user_id, content) 
                VALUES (%s, %s)
            """
            await cursor.execute(sql, (user_id, content))
        await connection.commit()
        return True
    except Exception as e:
        if connection:
            await connection.rollback()
        print(f"添加日志失败: {str(e)}")
        return False
    finally:
        await close_connection(connection)
